package com.byxy.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.byxy.entity.Forder;
import com.byxy.entity.Product;
import com.byxy.entity.Sorder;
import com.byxy.entity.User;
import com.byxy.service.ForderService;
import com.byxy.service.ProductService;
import com.byxy.service.SorderService;
import com.opensymphony.xwork2.ActionSupport;

public class DetailAction extends ActionSupport{

	@Resource
	private ProductService productService;
	
	@Resource
	private SorderService sorderService;
	
	@Resource
	private ForderService forderService;
	
	private Product product;
	
	private Integer id;
	
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}	
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String todetail() {
		System.out.println(id);
		product=productService.get(id);
		return "detail";
	}
	
	public String addSorder() {
		product=productService.get(product.getId());
		
		
		User u = (User) ServletActionContext.getRequest().getSession().getAttribute("u");
		List<Forder> forderlist=forderService.listByUser(u.getId());
		
		Sorder s=new Sorder();
		
		s.setName(product.getName());
		s.setPrice(product.getPrice());
		s.setNumber(1);
		s.setProduct(product);
				
		for(Forder f:forderlist) {
			s.setForder(f);
			break;
		}				
		
		sorderService.add(s);
		
		long num=0;
		for(Forder f:forderlist) {
			num+=sorderService.getSorder(f.getId());			
		}
		ServletActionContext.getRequest().getSession().setAttribute("number", num);
		
		
		return "detail";
		
	}
}
